var rteeImgPath = rootPath + "/tree/images/";
function rTree(objectName) {
	this.obj = objectName;
	this.icons = {
		plus : rteeImgPath + "nolines_plus.gif",
		minus : rteeImgPath + "nolines_minus.gif",
		empty : rteeImgPath + "empty.gif",
		joinLine : rteeImgPath + "join.gif",
		joinBottom : rteeImgPath + "joinbottom.gif",
		line : rteeImgPath + "line.gif",
		plusLine : rteeImgPath + "plus.gif",
		minusLine : rteeImgPath + "minus.gif",
		plusBottom : rteeImgPath + "plusbottom.gif",
		minusBottom : rteeImgPath + "minusbottom.gif"
	};
	this.folder = {
		fopen : rteeImgPath + "folderopen.gif",
		fclose : rteeImgPath + "folderclose.gif"
	};
	this.list = [];
	this.treeMap = [];
	this.hasBgImg = false;
	this.bgSrc = null;
	this.treeStr = "";
	// 根是否可以双击改变状态
	this.nodedbc = true;
	// 根是否要绑定事件
	this.nodehref = true;
	// 是否使用延迟加载，延迟加载的话，节点默认都是关闭
	this.lazy = false;
	// 是否使用点击节点改变节点背景
	this.alterbg = false;
	// 是否有复选框
	this.ischeckbox = false;
	// 复选框选中的值
	this.ischecked = [];
	// 复选框是否只能单选
	this.isSingle = false;
	// 是否显示下拉列表
	this.leafSelect = false;
	// 下拉列表默认选中值
	this.leafSelctDefault = [];
	// 下拉列表的options
	this.leafOptions = ['X', 'Z'];
	//--点击节点是否执行函数校验
	this.valideFun = false;
	//--执行函数的名称
	this.funName = "";
	//--是否显示名称提示信息
	this.tipFlag = false;
};

// 设置根的几个属性
rTree.prototype.setNodedbc = function(bool) {
	this.nodedbc = bool;
}
rTree.prototype.setNodehref = function(bool) {
	this.nodehref = bool;
}
rTree.prototype.setLazy = function(bool) {
	this.lazy = bool;
}
rTree.prototype.canAlterbg = function(bool) {
	this.alterbg = bool;
}

// 节点
// 参数 id，父id，显示字符，关闭时图片路径，打开时图片路径，href的路劲/调的js方法，目标，是否关闭（lazy=true时无效）
function leaf(id, pid, value, sel, src1, src2, url, target, isClose,tipInfo) {
	this.id = id;
	this.pid = pid;
	this.value = value;
	// 自定义图标的设置
	// 没有一号图标，就使用默认的文件夹图标
	if (!src1) {
		this.src1 = rteeImgPath + "folderclose.gif";
		this.src2 = rteeImgPath + "folderopen.gif";
		// this.src3=rtreeImgPath+"page.gif";
	}
	// 有一号图标，看2号有没有，没有的话2号图标等于自定义图标1
	else if (src1) {
		this.src1 = src1;
		this.src2 = src2 ? src2 : src1;
	}
	this.url = url ? url : null;
	this.target = target ? target : null;
	this.isClose = isClose ? isClose : false;
	this.tipInfo = tipInfo ? tipInfo :"";
};
// 添加节点
rTree.prototype.add = function(id, pid, value, sel, src, url, target, isClose,tipInfo) {
	//this.list[id] = new leaf(id, pid, value, src, url, target, isClose,tipInfo);//--不知道啥意思
	this.list[id] = new leaf(id, pid, value, sel, null, null, url, target, isClose,tipInfo);
};
// 转换成需要的数据类型
rTree.prototype.toTreeMap = function() {
	for (i in this.list) {
		var id = this.list[i].id;
		var pid = this.list[i].pid;
		if (this.treeMap[pid] == null) {
			this.treeMap[pid] = [];
		}
		this.treeMap[pid][id] = this.list[i];
	}
};
// 造树
rTree.prototype.makeTreeHasLine = function(pid, leftHtml) {
	var count = 1;
	var size = this.sonSize(this.treeMap[pid]);
	for (id in this.treeMap[pid]) {
		// 当使用延迟加载后，treeMap中的单个节点会多一个marginLeft键
		// 遇到时跳过
		if (this.lazy == true && id == "marginLeft") {
			continue;
		}
		// 父节点Div，1.2.0版本要使用的对象，现在已经过时
		// father=document.getElementById("tDiv"+pid);
		// 有子元素
		hasSon = false;
		// 是否时底部，当使用了延迟加载后，在不是最根节点的情况下，size要-1
		isBottom = (this.lazy && pid != 0)
				? (count == size - 1)
				: (count == size);
		// 判断是否有子节点
		if (this.treeMap[id] != null) {
			hasSon = true;
		}
		// 节点对象
		node = this.treeMap[pid][id];
		dkey = "tDiv" + id;
		ikey = "tImg" + id;
		// 图标的id
		ikeyT = "tImgT" + id;
		marginLeft = leftHtml;
		useMinus = isBottom ? this.icons.minusBottom : this.icons.minusLine;
		useLine = isBottom ? this.icons.joinBottom : this.icons.joinLine;
		// 控制按钮
		control = hasSon
				? "<img class=\"Img_Tree_C\" src='" + useMinus + "' id='"
						+ ikey + "' onclick='" + this.obj + ".alterTree(&quot;"
						+ id + "&quot;)' class='Img_Tree_Cur'/>"
				: "<img class=\"Img_Tree_C\" src='" + useLine + "'/>";
		src = node.src1;

		if (hasSon && !(this.lazy || node.isClose)) {
			src = node.src2;
		}

		// 图标
		if (hasSon) {
			icon = "<img class=\"Img_Tree_C\" id='" + ikeyT + "' src='" + src
					+ "' style='height:18px;width:18px'/>";
		} else {
			icon = "<img class=\"Img_Tree_C\" id='" + ikeyT + "' src='"
					+ rteeImgPath
					+ "/page.gif' style='height:18px;width:18px'/>";
		}
		// 内容与跳转目标
		target = node.target ? "target='" + node.target + "'" : "";
		dbclick = "";
		if (this.nodedbc) {
			dbclick = "ondblclick='" + this.obj + ".alterTree(&quot;" + id
					+ "&quot;)'";
		}
		on_click = ""; //--单击事件
		if (this.nodedbc && this.valideFun) {
			on_click = " onclick='"+this.funName+"'";
		}
		tip_info = ""; //--鼠标提示
		if (this.nodedbc && this.tipFlag) {
			tip_info = " title='"+node.tipInfo+"'";
		}
		rootHref = "";
		if (this.nodehref && hasSon) {
			rootHref = "href='" + node.url + "' ";
		}
		var checkboxes = "";
		if (this.ischeckbox) {
			checkboxes = "<span><input type='checkbox' nodeid='" + node.id
					+ "' nodepid='" + node.pid + "' nodename='" + node.value
					+ "' id='tDiv" + node.pid + "' "
					+ (this.contain(node.id) ? "checked" : " ")
					+ " onclick='return "
					+ (this.isSingle ? "single(this)" : "ischeck(this)")
					+ ";'></span>";
		}

		var leafs = "";
		if (!hasSon && this.leafSelect) {
			leafs = "&nbsp;&nbsp;&nbsp;<span><select id='sel" + node.id + "'>";
			var isinner = true;
			for (var k in this.leafSelctDefault) {
				if (this.leafSelctDefault[k][0] == node.id) {
					for (var j in this.leafOptions) {
						leafs += "<option value='" + j + "' "
								+ (j == this.leafSelctDefault[k][1] ? 'selected' : '') + ">"
								+ this.leafOptions[j] + "</option>";
					}
					isinner = false;
				}
			}
			if(isinner){
				for (var j in this.leafOptions) {
					leafs += "<option value='" + j + "'>" + this.leafOptions[j] + "</option>";
				}
			}
			leafs += "</select></span>";
		}

		if (node.url) {
			title = (!hasSon)
					? "<span class='treeTitle' nodeid='" + node.id
							+ "' nodepid='" + node.pid + "'><a href='"
							+ node.url + "' " + target +  on_click + tip_info +">" + node.value
							+ "</a></span>"
					: "<span nodeid='" + node.id + "' nodepid='" + node.pid
							+ "' class='treeTitle' " + dbclick +"><a "
							+ rootHref + " " + target + ">" + node.value
							+ "</a></span>";
		} else {
			title = (!hasSon)
					? "<span class='treeTitle' nodeid='" + node.id
							+ "' nodepid='" + node.pid + "'>" + node.value
							+ "</span>"
					: "<span nodeid='" + node.id + "' nodepid='" + node.pid
							+ "' class='treeTitle' " + dbclick + tip_info +">"
							+ node.value + "</span>";
		}
		// 拼接
		str = marginLeft
				+ control
				+ icon
				+ checkboxes
				+ title
				+ leafs
				+ "<br>"
				+ (hasSon
						? "<div id='" + dkey + "' style='display:block'>"
						: "");
		// 由于要使用文件夹格式，临时改成这样，默认全是关闭的文件夹图标
		// 有子节点，默认是打开，所以换成打开的坐标图片
		if (hasSon && (this.lazy || node.isClose)) {
			// 层隐藏，图标加号
			str = str.replace("block", "none");
			str = str.replace("minus", "plus");
		}
		this.treeStr += str;
		if (hasSon) {
			useEmpty = isBottom ? this.icons.empty : this.icons.line;
			marginLeft += "<img class=\"Img_Tree_C\" src='" + useEmpty + "'/>";
			if (this.lazy) {
				this.treeMap[id]["marginLeft"] = marginLeft;
			} else {
				this.makeTreeHasLine(id, marginLeft);
			}
			this.treeStr += "</div>";
		}
		count++;
	}
};
// 子元素的数量
rTree.prototype.sonSize = function(map) {
	var size = 0;
	for (obj in map) {
		size++;
	}
	return size;
};
// 改变节点的显示与隐藏
rTree.prototype.alterTree = function(id) {
	divTarget = document.getElementById("tDiv" + id);
	if (divTarget.style.display == "block") {
		this.closeNode(id);
	} else {
		this.openNode(id);
	}
};
// 打开节点
rTree.prototype.openNode = function(id) {
	var divTarget = document.getElementById("tDiv" + id);
	var imgTarget = document.getElementById("tImg" + id);
	var iconTarget = document.getElementById("tImgT" + id);
	divTarget.style.display = "block";
	// 不管是哪种类型的加减号，转换字符串就行了
	imgTarget.src = imgTarget.src.replace("plus", "minus");
	// 文件夹图标的打开
	iconTarget.src = this.list[id].src2;
	if (this.lazy == true && divTarget.innerHTML == '') {
		this.treeStr = "";
		this.makeTreeHasLine(id, this.treeMap[id]["marginLeft"]);
		divTarget.innerHTML += this.treeStr;
		if (this.alterbg) {
			alterBg();
		}
	}
};
// 关闭节点
rTree.prototype.closeNode = function(id) {
	var divTarget = document.getElementById("tDiv" + id);
	var imgTarget = document.getElementById("tImg" + id);
	var iconTarget = document.getElementById("tImgT" + id);
	divTarget.style.display = "none";
	imgTarget.src = imgTarget.src.replace("minus", "plus");
	// 文件夹图标的关闭
	iconTarget.src = this.list[id].src1;

};
// 打开所有节点
rTree.prototype.openTree = function() {
	for (id in this.treeMap) {
		if (id == 0) {
			continue;
		}
		this.openNode(id);
	}
};
// 关闭所有节点
rTree.prototype.closeTree = function() {
	for (id in this.treeMap) {
		if (id == 0) {
			continue;
		}
		// 在延迟加载的情况下，节点可能还未生成，自然不能被关闭
		if (this.lazy == true) {
			var divTarget = document.getElementById("tDiv" + id);
			if (divTarget == null)
				continue;
		}
		this.closeNode(id);
	}
};
// 使用背景图片
rTree.prototype.useBgImg = function(src) {
	this.bgSrc = src;
	this.hasBgImg = true;
};
// 开始树的生成
rTree.prototype.startTree = function() {
	var mainDiv = document.getElementById("main_Tree_DIV");
	this.treeStr = "<div id='T_DIV_TREE_MUDEL'>";
	if (this.hasBgImg) {
		var bgStr = '<img class="Img_Tree_C" src="'
				+ this.bgSrc
				+ '" width="100%" height="100%" style="position:absolute;z-index:-1;"/>';
		mainDiv.innerHTML = bgStr + mainDiv.innerHTML;
	}
	this.toTreeMap();
	this.makeTreeHasLine(0, "");
	this.treeStr += "</div>";

	mainDiv.innerHTML += this.treeStr;

	if (this.alterbg) {
		alterBg();
	}
};
rTree.prototype.contain = function(o) {
	for (var item in this.ischecked) {
		if (this.ischecked[item] == o) {
			return true;
		}
	}
	return false;
};
function single(o) {
	inerDivCheck = $("#main_Tree_DIV").find("input:checkbox");
	inerDivCheck.each(function(i) {
				this.checked = false;
			});
	o.checked = true;
};

function ischeck(o) {
	// var allCheck = $("#T_DIV_TREE_MUDEL").find("input:checkbox");
	var inerDivCheck;
	// if(o.nodepid==0){
	// allCheck.each(function(i){this.checked = o.checked;});
	// }else{
	inerDivCheck = $("#tDiv" + o.nodeid).find("input:checkbox");
	inerDivCheck.each(function(i) {
				this.checked = o.checked;
			});
	// }
};

function alterBg() {
	$('.treeTitle').unbind('click').click(function() {
				$('.bgGray').removeClass('bgGray');
				$(this).addClass('bgGray');
			});
};